#include "LevelBlockFactory.h"


#include "LevelBLockGeneric.h"

#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>

#include "OgreFramework.h"

#include "GameState.h"

#include "CFGLoader.h"

#include <map>

LevelBlockFactory::LevelBlockFactory(LevelManager* _parent):
    m_parent(_parent)
{
    //ctor


}

LevelBlockFactory::~LevelBlockFactory()
{
    //dtor
}






/** @brief createGenricBlock
  *
  * @todo: document this function
  */
LevelBlock* LevelBlockFactory::createGenericBlock(std::string name, int i, int j)
{
    Ogre::SceneManager *sceneMgr = OgreFramework::getSingletonPtr()->m_pRoot->getSceneManager("GameSceneMgr");
    Ogre::SceneNode *node=0;
    Ogre::Entity* entity=0;
    LevelBlock* block=0;

    std::map<std::string, std::string> params = CFGLoader::loadRessourceFile(name);
//    OgreFramework::getSingletonPtr()->m_pLog->logMessage("Loading block : "+params["Name"]);

    block = new LevelBlockGeneric();


    Ogre::Plane plane;
    plane.normal = Ogre::Vector3::UNIT_Y;
    plane.d = 0;

    Ogre::String str,str2,str3;

    str = params["Name"];
    str2 = str.append("_i");
    str2 = str2.append(Ogre::StringConverter::toString(i));
    str2 = str2.append("j");
    str2 = str2.append(Ogre::StringConverter::toString(j));

    str3 = "block";
    str3 = str3.append("i");
    str3 = str3.append(Ogre::StringConverter::toString(i));
    str3 = str3.append("j");
    str3 = str3.append(Ogre::StringConverter::toString(j));


    Ogre::MeshManager::getSingleton().createPlane(str2, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1.0f, 1.0f, 1, 1, true, 1, 1.0f, 1.0f, Ogre::Vector3::UNIT_Z);
    entity = sceneMgr->createEntity(str3, str2);
    entity->setMaterialName(params["MatScript"]);
    entity->setCastShadows(false);
    entity->setQueryFlags(QUERY_BLOCK);

    node = sceneMgr->getRootSceneNode()->createChildSceneNode();
    node->attachObject(entity);
    node->setPosition(i,-1.5,j);


    block->setNode(node);
//    block->setEntity(entity);
    block->setPosition(Ogre::Vector2(i,j));
    block->setName(params["Name"]);



  //  OgreFramework::getSingletonPtr()->m_pLog->logMessage(params["Name"]);


    if (params["Crossable"].compare("1")==0)
    {
        block->setCrossable(true);
    }
    else
    {

        block->setCrossable(false);
    }

    return block;
}



